library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr)
library(spatstat)
## Loading required package: spatstat.data
## Loading required package: spatstat.geom
## spatstat.geom 2.4-0
## Loading required package: spatstat.random
## spatstat.random 2.2-0
## Loading required package: spatstat.core
## Loading required package: nlme
##
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
##
## collapse
## Loading required package: rpart
## spatstat.core 2.4-4
## Loading required package: spatstat.linnet
## spatstat.linnet 2.3-2
##
## spatstat 2.3-4 (nickname: 'Watch this space')
## For an introduction to spatstat, type 'beginner'
library(ggplot2)
library(ggExtra)
library(EBImage)
##
## Attaching package: 'EBImage'
## The following objects are masked from 'package:spatstat.geom':
##
## affine, closing, distmap, opening, rotate
df<-read_csv("/Users/hainanxu/Documents/spatial_visual_cortex/docs/data/H07-0500_79205589_179_MBP.csv")
im4=readImage("/Users/hainanxu/Documents/spatial_visual_cortex/docs/data/179.jpeg")
ln = with(df,
ppp(x = com_x, y = com_y,marks=pixel_area, xrange = range(com_x), yrange = range(com_y)))
qq=quadratcount(ln,nx=3,ny=12)
par(mfrow=c(1,3))
#display(im4,method="raster")
plot(ln,main="")
plot(qq,main='')
quadrat.test(qq)
##
## Chi-squared test of CSR using quadrat counts
##
## data:
## X2 = 483.24, df = 35, p-value < 2.2e-16
## alternative hypothesis: two.sided
##
## Quadrats: 3 by 12 grid of tiles

d = density(ln, edge=TRUE)
plot(d,main="")

x<- df$com_y
y <- df$com_x
library(MASS)
ln_3d <- kde2d(x, y)
persp(ln_3d, box=FALSE)

??persp
library(plotly)
plot_ly(x=ln_3d$x, y=ln_3d$y, z=ln_3d$z) %>% add_surface()
eG = envelope(ln, fun=Gest, nsim=200,nrank=1)
## Generating 200 simulations of CSR ...
## 1, 2, 3, 4.6.8.10.12.14.16.18.20.22.24.26.28.30.32.34.36.38.40
## .42.44.46.48.50.52.54.56.58.60.62.64.66.68.70.72.74.76.78.80
## .82.84.86.88.90.92.94.96.98.100.102.104.106.108.110.112.114.116.118.120
## .122.124.126.128.130.132.134.136.138.140.142.144.146.148.150.152.154.156.158.160
## .162.164.166.168.170.172.174.176.178.180.182.184.186.188.190.192.194.196.198. 200.
##
## Done.
plot(eG,xlim=c(0,5),main='')

eK = envelope(ln, fun=Kinhom, nsim=200,nrank=1)
## Generating 200 simulations of CSR ...
## 1, 2, 3, 4.6.8.10.12.14.16.18.20.22.24.26.28.30.32.34.36.38.40
## .42.44.46.48.50.52.54.56.58.60.62.64.66.68.70.72.74.76.78.80
## .82.84.86.88.90.92.94.96.98.100.102.104.106.108.110.112.114.116.118.120
## .122.124.126.128.130.132.134.136.138.140.142.144.146.148.150.152.154.156.158.160
## .162.164.166.168.170.172.174.176.178.180.182.184.186.188.190.192.194.196.198. 200.
##
## Done.
plot(eK,xlim=c(0,80),main='')

plot(eK,xlim=c(0,6),main='')

eL=envelope(ln, fun=Linhom, nsim=200,nrank=1)
## Generating 200 simulations of CSR ...
## 1, 2, 3, 4.6.8.10.12.14.16.18.20.22.24.26.28.30.32.34.36.38.40
## .42.44.46.48.50.52.54.56.58.60.62.64.66.68.70.72.74.76.78.80
## .82.84.86.88.90.92.94.96.98.100.102.104.106.108.110.112.114.116.118.120
## .122.124.126.128.130.132.134.136.138.140.142.144.146.148.150.152.154.156.158.160
## .162.164.166.168.170.172.174.176.178.180.182.184.186.188.190.192.194.196.198. 200.
##
## Done.
plot(eL,xlim=c(0,80),main='')

plot(eL,xlim=c(0,6),main='')

pcfln = pcf(Kinhom(ln))
## number of data points exceeds 1000 - computing border correction estimate only
plot(pcfln)

plot(pcfln,xlim=c(0,25),main="")

mybreaks<-hist(d,breaks=100,xlim=c(0,1e-03),xlab="Estimated density",ylab="Frequency","")$breaks

mycolors<-rep("red",length(mybreaks))
#mycolors[mybreaks>=3e-04]<-'grey'
mycolors[mybreaks>=3e-04&mybreaks<=6e-04]<-'blue'
mycolors[mybreaks>=6e-04&mybreaks<6.75e-04]<-'green'
mycolors[mybreaks>=6.75e-04]<-'yellow'
#my colors[mybreaks>8e-04]<-'grey'
hist(d,breaks=100,xlim=c(0,1e-03),xlab="Estimated density",ylab="Frequency","",col=mycolors,main="")
abline(v = 3e-04, col="red", lwd=2, lty=2)
abline(v = 6e-04, col="blue", lwd=2, lty=2)
abline(v = 6.8e-04, col="green", lwd=2, lty=2)

#abline(v = 8.1e-04, col="yellow", lwd=2, lty=2)
par(mfrow=c(1,5))
plot(d,main='')
plot(d<3e-04,main="Region 1(red)")
plot(d>3e-04&d<6e-04,main="Region 2(blue)")
plot(d>6e-04&d<6.75e-04,main="Region 3 (green)")
plot(d>6.75e-04,main="Region 4(yellow)")

ln2 = with(df,
ppp(x = com_x,y = com_y, xrange = range(com_x), yrange = range(com_y)))
## Warning: data contain duplicated points
plot(ln2,pch=20)
plot((ln2[density(ln2)<3e-04]),add=TRUE,col="red",pch=20)

plot(ln2,pch=20)
plot((ln2[density(ln2)>3e-04&density(ln2)<6e-04]),add=TRUE,col="blue",pch=20)

plot(ln2,pch=20)
plot((ln2[density(ln2)>6e-04&density(ln2)<6.75e-04]),add=TRUE,col='green',pch=20)

plot(ln2,pch=20)
plot((ln2[density(ln2)>6.75e-04]),add=TRUE,col='yellow',pch=20)

redG=envelope(ln[d<3e-04],fun=Gest,nsim=50,nrand=1)
## Generating 50 simulations of CSR ...
## 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
## 41, 42, 43, 44, 45, 46, 47, 48, 49, 50.
##
## Done.
plot(redG)

redK=envelope(ln[d<3e-04],fun=Kest,nsim=200,nrand=1)
## Generating 200 simulations of CSR ...
## 1, 2, 3, 4.6.8.10.12.14.16.18.20.22.24.26.28.30.32.34.36.38.40
## .42.44.46.48.50.52.54.56.58.60.62.64.66.68.70.72.74.76.78.80
## .82.84.86.88.90.92.94.96.98.100.102.104.106.108.110.112.114.116.118.120
## .122.124.126.128.130.132.134.136.138.140.142.144.146.148.150.152.154.156.158.160
## .162.164.166.168.170.172.174.176.178.180.182.184.186.188.190.192.194.196.198. 200.
##
## Done.
plot(redK)

blueG=envelope(ln[d>3e-04&d<6e-04],fun=Gest,nsim=200,nrand=1)
## Generating 200 simulations of CSR ...
## 1, 2, 3, 4.6.8.10.12.14.16.18.20.22.24.26.28.30.32.34.36.38.40
## .42.44.46.48.50.52.54.56.58.60.62.64.66.68.70.72.74.76.78.80
## .82.84.86.88.90.92.94.96.98.100.102.104.106.108.110.112.114.116.118.120
## .122.124.126.128.130.132.134.136.138.140.142.144.146.148.150.152.154.156.158.160
## .162.164.166.168.170.172.174.176.178.180.182.184.186.188.190.192.194.196.198. 200.
##
## Done.
plot(blueG)

blueK=envelope(ln[d>3e-04&d<6e-04],fun=Kest,nsim=200,nrand=1)
## Generating 200 simulations of CSR ...
## 1, 2, 3, 4.6.8.10.12.14.16.18.20.22.24.26.28.30.32.34.36.38.40
## .42.44.46.48.50.52.54.56.58.60.62.64.66.68.70.72.74.76.78.80
## .82.84.86.88.90.92.94.96.98.100.102.104.106.108.110.112.114.116.118.120
## .122.124.126.128.130.132.134.136.138.140.142.144.146.148.150.152.154.156.158.160
## .162.164.166.168.170.172.174.176.178.180.182.184.186.188.190.192.194.196.198. 200.
##
## Done.
plot(blueK)

qqblue=quadratcount(ln[d>3e-04&d<6e-04],nx=3,ny=8)
quadrat.test(qqblue)
##
## Chi-squared test of CSR using quadrat counts
##
## data:
## X2 = 16.445, df = 6, p-value = 0.02311
## alternative hypothesis: two.sided
##
## Quadrats: 7 tiles (levels of a pixel image)
plot(qqblue)

greenG=envelope(ln[d>6e-04&d<6.75e-04],fun=Gest,nsim=200,nrand=1)
## Generating 200 simulations of CSR ...
## 1, 2, 3, 4.6.8.10.12.14.16.18.20.22.24.26.28.30.32.34.36.38.40
## .42.44.46.48.50.52.54.56.58.60.62.64.66.68.70.72.74.76.78.80
## .82.84.86.88.90.92.94.96.98.100.102.104.106.108.110.112.114.116.118.120
## .122.124.126.128.130.132.134.136.138.140.142.144.146.148.150.152.154.156.158.160
## .162.164.166.168.170.172.174.176.178.180.182.184.186.188.190.192.194.196.198. 200.
##
## Done.
plot(greenG)

yellowG=envelope(ln[d>6.75e-04],fun=Gest,nsim=200,nrand=1)
## Generating 200 simulations of CSR ...
## 1, 2, 3, 4.6.8.10.12.14.16.18.20.22.24.26.28.30.32.34.36.38.40
## .42.44.46.48.50.52.54.56.58.60.62.64.66.68.70.72.74.76.78.80
## .82.84.86.88.90.92.94.96.98.100.102.104.106.108.110.112.114.116.118.120
## .122.124.126.128.130.132.134.136.138.140.142.144.146.148.150.152.154.156.158.160
## .162.164.166.168.170.172.174.176.178.180.182.184.186.188.190.192.194.196.198. 200.
##
## Done.
plot(yellowG)
